cmake_minimum_required(VERSION 3.0)

project(jklibs)

include_directories(common)
include_directories(common/misc)
include_directories(common/ex)
include_directories(common/funcs)
include_directories(third/jsoncpp)
include_directories(third)
include_directories(storage)
include_directories(codec)

include_directories(${X264_PATH}/include)
include_directories(${X265_PATH}/include)
include_directories(${OPENCV_PATH}/include)
include_directories(${OPENCV_PATH}/include/opencv4)
include_directories(${PLAT_PATH}/include)
link_directories(${X264_PATH}/lib)
link_directories(${X265_PATH}/lib)
link_directories(${OPENCV_PATH}/lib)
link_directories(${PLAT_PATH}/lib)

option(WINDOWS "windows platform" OFF)
option(LINUX "linux platform" OFF)
option(WITH_GCC_HIGH "gcc high version" ON)
option(WITH_OPENCV "opencv need 4.0" OFF)
option(CODEC "codec" OFF)
option(CMAUTOTEST "Auto test" OFF)
option(WITH_DATABASE "database function support" OFF)
option(WITH_LIBCURL "libcurl support" OFF)
option(WITH_MQTT "mqtt support" OFF)
option(WITH_COAP "coap suport" OFF)
option(WITH_QT "qt support" OFF)
option(WITH_VDEV "qt support" OFF)
option(WITH_DBUSCPP "DBus cpp function" OFF)

if (WITH_COAP)
	include_directories(${COAP_PATH}/include)
	link_directories(${COAP_PATH}/lib)
endif ()

if (WITH_GCC_HIGH)
	add_definitions(-DGCC_HIGH)
endif()

MESSAGE(STATUS "System is ${CMAKE_SYSTEM_NAME}")

if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
	set(LINUX ON)
endif()

if (WITH_LIBCURL)
	add_definitions(-DUSE_LIBCURL)
	include_directories(${LIBCURL_PATH}/include)
	link_directories(${LIBCURL_PATH}/lib)
	set(CURL_LIB curl)
endif()
if (WITH_MQTT)
	add_definitions(-DUSE_MQTT_MOSQ)
	include_directories(${MOSQ_PATH}/include)
	link_directories(${MOSQ_PATH}/lib)
endif()

IF (LINUX)
message(STATUS "This is linux platform")
ENDIF()

set(COMMON_EX_JSON_FILES
		third/jsoncpp/json/allocator.h
		third/jsoncpp/json/assertions.h
		third/jsoncpp/json/autolink.h
		third/jsoncpp/json/features.h
		third/jsoncpp/json/forwards.h
#		third/jsoncpp/json/config.h
		third/jsoncpp/json/json.h
		third/jsoncpp/json_reader.cpp
		third/jsoncpp/json/json_tool.h
		third/jsoncpp/json_value.cpp
		third/jsoncpp/json/json_valueiterator.inl
		third/jsoncpp/json_writer.cpp
		third/jsoncpp/json/reader.h
		third/jsoncpp/json/value.h
		third/jsoncpp/json/version.h
		third/jsoncpp/json/writer.h
		)

if (WITH_COAP)
	set(COMMON_COAP_FILES
			common/net/CMCoapServer.cpp
			common/net/CMCoapServer.h
			common/net/CMCoapBroadcast.cpp
			common/net/CMCoapBroadcast.h)
endif ()

set(COMMON_SNET_FILES
		common/net/CMMqttBase.cpp
		common/net/CMMqttBase.h
		common/net/CMMqttMosquitto.cpp
		common/net/CMMqttMosquitto.h
		)

set(COMMON_CONFIG_EX_FILES
		common/config/cm_conf_ex.cpp
		common/config/cm_conf_ex.h
		)

set(COMMON_CONFIG_FILES
	${COMMON_CONFIG_EX_FILES}
		common/config/CMConfigCpp.cpp
		common/config/CMConfigCpp.h
		common/config/CMConfigGeneral.cpp
		common/config/CMConfigGeneral.h
		common/config/CMConfigSimple.cpp
		common/config/CMConfigSimple.h
		common/funcs/CMCommandLineParser.cpp
		common/funcs/CMCommandLineParser.h
		${COMMON_EX_JSON_FILES})

set(COMMON_LOCK_FILES
		common/funcs/CMLock.cpp common/funcs/CMLock.h)

set(COMMON_EX_FILES
		common/funcs/CMObserver.cpp
		common/funcs/CMObserver.h
		)

set(COMMON_FUNCS_FILES
		common/funcs/CMTaskRunner.cpp common/funcs/CMTaskRunner.h
		common/funcs/CMTimeout.cpp common/funcs/CMTimeout.h)

set(COMMON_I18N_FILES
		common/i18n/CMLanguageConfig.cpp common/i18n/CMLanguageConfig.h)

set(COMMON_MSGTASK_FILES
		common/msg/CMMsgTaskInterface.cpp common/msg/CMMsgTaskInterface.h
		common/msg/CMMsgTaskManager.cpp common/msg/CMMsgTaskManager.h)

set(COMMON_FILES
		common/utils/cm_utils.c
		common/utils/cm_print.c
		common/crypto/cm_base64.c
		common/crypto/cm_base64.h
		common/utils/cm_print_cc.c
		common/utils/cm_print_cc.h
        ${COMMON_EX_FILES}
		${COMMON_FUNCS_FILES}
		${COMMON_I18N_FILES}
		${COMMON_MSGTASK_FILES}
		${COMMON_LOCK_FILES}
		)

set(H264_PARSE_FILES
		codec/h264.cpp)

IF (LINUX AND CODEC)
set(CODEC_FILES
        codec/codec.c
        codec/encoder.c
        codec/enc_h265.c
        codec/cmyuv.c)
ENDIF()

set(STORAGE_FILES
        storage/avstorage.c)

set(RECORDSERVER_FILES
        recordserver/cm_avprocess.c)

	set(VDEV_FILES
		vdev/operation.c
		vdev/vdev.c
		vdev/vdev.h
		vdev/base.h
		)

add_executable(cmyuvtools
		codec/cmyuvtools.c
		codec/cmyuv.c
		${COMMON_FILES}
		${COMMON_CONFIG_EX_FILES}
		)
	target_link_libraries(
		cmyuvtools
		pthread
		)

add_executable(avstorage_test
		storage/avstorage_test.c
		${STORAGE_FILES}
		${COMMON_FILES}
		${COMMON_CONFIG_EX_FILES}
		)
	target_link_libraries(
		avstorage_test
		pthread
		)

add_executable(h264_parse
		codec/h264_parse.cpp
		${H264_PARSE_FILES})

add_executable(fiforead
		common/misc/cm_fifo.c
		common/misc/cm_fifo.h
		common/utils/cm_print.c
		common/utils/cm_print.h
		common/utils/cm_logprint.h
		)

set_target_properties(fiforead
		PROPERTIES
		COMPILE_FLAGS    -D__MAINTEST
		)

target_link_libraries(fiforead
		pthread
		)

add_executable(cm_observer_test
		common/funcs/cm_observer_test.cpp
		${COMMON_FILES}
	${COMMON_CONFIG_EX_FILES}
	)
	target_link_libraries(
		cm_observer_test
		pthread
		)

if (WITH_GCC_HIGH)
#add_executable(cm_config_test
#		common/ex/cm_config_test.cpp
#		${COMMON_CONFIG_FILES}
#		${COMMON_FILES})
#	target_link_libraries(cm_config_test
#		pthread)
endif()

if (LINUX)
add_library(jklibs
        ${STORAGE_FILES}
        ${COMMON_FILES}
        ${RECORDSERVER_FILES}
        ${CODEC_FILES}
		)
endif()

IF (LINUX AND CODEC)
add_executable(cmcodec
		codec/cmcodec.c
		codec/cmyuv.c
		${CODEC_FILES}
		${COMMON_FILES}
		)
target_link_libraries(cmcodec
		x264 x265)

add_executable(avprocess_test
        recordserver/cm_avprocess_test.c
        ${RECORDSERVER_FILES}
        ${CODEC_FILES}
        ${STORAGE_FILES}
        ${COMMON_FILES})

target_link_libraries(avprocess_test
        x264
        x265)
ENDIF()

IF (WITH_OPENCV)
add_subdirectory(openav)
ENDIF()

IF (CMAUTOTEST)
	set(CMAUTOTESTFILES
		common/autotest/CMAutoTestCommand.cpp
		common/autotest/CMAutoTestCommand.h
		common/autotest/CMCommandInstance.cpp
		common/autotest/CMCommandInstance.h
		common/autotest/CMFifoHandler.cpp
		common/autotest/CMFifoHandler.h
		common/autotest/CMTestKeyCommand.cpp
		common/autotest/CMTestKeyCommand.h
		common/misc/cm_fifo.c
		common/misc/cm_fifo.h
			common/utils/cm_print.c
			common/utils/cm_print.h
			common/utils/cm_logprint.h
		)
	add_executable(cmautotest
		common/autotest/cmautotest.cpp
		${CMAUTOTESTFILES}
		)
	target_link_libraries(cmautotest
		pthread
		)
ENDIF()

add_subdirectory(codec)
#add_subdirectory(utils)

IF (WITH_DATABASE)
	add_subdirectory(database)
ENDIF()

IF (WITH_QT)
	add_subdirectory(showWindow)
ENDIF()

add_executable(cm_msg_task_demo
        common/msg/cm_msg_task_test.cpp
		${COMMON_MSGTASK_FILES}
		${COMMON_LOCK_FILES})
target_link_libraries(cm_msg_task_demo
		pthread)

add_executable(cm_command_parse_test
		common/funcs/cm_command_parse_test.cpp
		common/funcs/CMCommandLineParser.cpp
		common/funcs/CMCommandLineParser.h
		common/utils/cm_print_cc.c
		common/utils/cm_print_cc.h)

if (WITH_VDEV)
	add_executable(vdev_demo
		${VDEV_FILES}
		vdev/demo.c
			common/utils/cm_print.c
			common/utils/cm_print.h
		)
endif()

add_subdirectory(base_study)
add_subdirectory(common)


